# 豆瓣音乐Top250爬虫
from typing import Iterable

import scrapy
from scrapy import Request
from ..items import MyDoubanMusicItem


class DoubanMusicSpider(scrapy.Spider):
    name = 'DoubanMusicSpider'

    allowed_domains = ['music.douban.com']

    start_urls = []
    base_url = 'https://music.douban.com/top250?start={}'
    for i in range(1, 11):
        start_urls.append(base_url.format((i - 1) * 25))

    # 自定义请求头来伪装成用户请求目标的网站
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
    }

    # 重新实现start_requests方法，手动发起第一个请求
    # 手动发起请求就可以指定请求头
    def start_requests(self) -> Iterable[Request]:
        # 检查是否定义了 start_urls
        if not self.start_urls and hasattr(self, "start_url"):
            raise AttributeError(
                "Crawling could not start: 'start_urls' not found "
                "or empty (but found 'start_url' attribute instead, "
                "did you miss an 's'?)"
            )
        # 遍历start_urls，依次的发起请求
        for url in self.start_urls:
            yield Request(url, headers=self.headers, dont_filter=True)

    def parse(self, response):
        # 提取每一页的音乐排名信息
        # cnt = 0
        for tr in response.xpath("//div[@class='indent']//tr[@class='item']"):
            # cnt += 1
            name = tr.xpath(".//div[@class='pl2']/a/text()").get().strip()
            infos = tr.xpath(".//div[@class='pl2']/p/text()").get().strip().split(" / ")
            author = infos[0]
            date = infos[1]
            quality = infos[-2]
            style = infos[-1]
            if len(infos) == 5:
                category = infos[2]
            else:
                category = ''

            rating_nums = tr.xpath(".//div[@class='pl2']//span[@class='rating_nums']/text()").get().strip()


            # 构建Item
            item = MyDoubanMusicItem()
            item['name'] = name
            item['author'] = author
            item['date'] = date
            item['category'] = category
            item['quality'] = quality
            item['style'] = style
            item['rating_nums'] = rating_nums
            yield item
        # print("当前页面共%d个item" % cnt)
